﻿using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace Version3.SharePoint
{
    //Very simple property editor, 
    //collects data from user, no error checking

    [CLSCompliant(false)]
    [Guid("4D54B1BE-DA47-4478-B48D-B1EDB7651512")]
    public class ContentLookupPropertyEditor : UserControl, IFieldEditor
    {
        protected TextBox txtSourceWebURL;
        protected DropDownList cboSourceListName;
        protected DropDownList cboColumnOne;
        protected DropDownList cboColumnTwo;
        protected DropDownList cboColumnThree;
        protected ImageButton btnRefreshList;

        private string _WebURL = string.Empty;
        private string _ListName = string.Empty;
        private string _FieldOne = string.Empty;
        private string _FieldTwo = string.Empty;
        private string _FieldThree = string.Empty;

        public override void Focus()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (!Page.IsPostBack)
            {
                txtSourceWebURL.Text = _WebURL;
                LoadComboBoxes(_ListName);
                LoadComboBoxes(null);
                try
                {
                    cboSourceListName.SelectedValue = _ListName;
                    cboColumnOne.SelectedValue = _FieldOne;
                    cboColumnTwo.SelectedValue = _FieldTwo;
                    cboColumnThree.SelectedValue = _FieldThree;
                }
                catch { }
            }
            btnRefreshList.Click += new ImageClickEventHandler(btnRefreshList_Click);
            cboSourceListName.SelectedIndexChanged += new EventHandler(cboSourceListName_SelectedIndexChanged);

        }

        void btnRefreshList_Click(object sender, ImageClickEventArgs e)
        {
            _ListName = string.Empty;
            _FieldOne = string.Empty;
            _FieldTwo = string.Empty;
            _FieldThree = string.Empty;
            cboSourceListName.Items.Clear();
            cboSourceListName.Items.Add(string.Empty);
            cboSourceListName.SelectedValue = string.Empty;
            cboColumnOne.Items.Clear();
            cboColumnTwo.Items.Clear();
            cboColumnThree.Items.Clear();
            LoadComboBoxes(null); 
        }

        void cboSourceListName_SelectedIndexChanged(object sender, EventArgs e)
        {
            _FieldOne = string.Empty;
            _FieldTwo = string.Empty;
            _FieldThree = string.Empty;
            cboColumnOne.Items.Clear();
            cboColumnTwo.Items.Clear();
            cboColumnThree.Items.Clear();
            cboColumnOne.Items.Add(string.Empty);
            cboColumnOne.SelectedValue = string.Empty;
            cboColumnTwo.Items.Add(string.Empty);
            cboColumnTwo.SelectedValue = string.Empty;
            cboColumnThree.Items.Add(string.Empty);
            cboColumnThree.SelectedValue = string.Empty;
            LoadComboBoxes(cboSourceListName.SelectedValue);
        }


        void LoadComboBoxes(string listMatch)
        {
            if (!string.IsNullOrEmpty(txtSourceWebURL.Text))
            {
                using (SPSite topSite = new SPSite(txtSourceWebURL.Text))
                {
                    using (SPWeb topWeb = topSite.OpenWeb())
                    {
                        foreach (SPList webList in topWeb.Lists)
                        {
                            if (string.IsNullOrEmpty(listMatch))
                            {
                                cboSourceListName.Items.Add(webList.Title);
                            }
                            else
                            {
                                if (webList.Title == listMatch)
                                {
                                    for (int fieldIndex = 0; fieldIndex < webList.Fields.Count; fieldIndex++)
                                    {
                                        try
                                        {
                                            SPField listField = webList.Fields[fieldIndex];
                                            if (listField.Type == SPFieldType.Text || listField.Type == SPFieldType.Note)
                                            {
                                                cboColumnOne.Items.Add(listField.Title);
                                                cboColumnTwo.Items.Add(listField.Title);
                                                cboColumnThree.Items.Add(listField.Title);
                                            }
                                        }
                                        catch (Exception fieldRegistrationError)
                                        {
                                            System.Diagnostics.Trace.WriteLine(fieldRegistrationError.Message);
                                        }
                                    }
          
                                }
                            }
                        }
                    }
                }
            }
        }

        #region IFieldEditor Members

        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            if (null != field)
            {
                ContentLookupField sharePointField = (ContentLookupField)field;
                _WebURL = sharePointField.WebURL; ;
                _ListName = sharePointField.ListName;
                _FieldOne = sharePointField.FieldOne;
                _FieldTwo = sharePointField.FieldTwo;
                _FieldThree = sharePointField.FieldThree;
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            ContentLookupField sharePointField = (ContentLookupField)field;
            _WebURL = txtSourceWebURL.Text;
            _ListName = cboSourceListName.SelectedValue;
            _FieldOne = cboColumnOne.SelectedValue;
            _FieldTwo = cboColumnTwo.SelectedValue;
            _FieldThree = cboColumnThree.SelectedValue;

            sharePointField.WebURL = _WebURL;
            sharePointField.ListName = _ListName;
            sharePointField.FieldOne = _FieldOne;
            sharePointField.FieldTwo = _FieldTwo;
            sharePointField.FieldThree = _FieldThree;
            if (isNewField)
            {
                //if the field is new, we will set the global new property cache
                //which helps us avoid all of the conflicts between update states
                //that can occur during postback processing of the control
                sharePointField.SetNewColumnPropertyCacheValue("WebURL", _WebURL);
                sharePointField.SetNewColumnPropertyCacheValue("ListName", _ListName);
                sharePointField.SetNewColumnPropertyCacheValue("FieldOne", _FieldOne);
                sharePointField.SetNewColumnPropertyCacheValue("FieldTwo", _FieldTwo);
                sharePointField.SetNewColumnPropertyCacheValue("FieldThree", _FieldThree);
            }

        }

        #endregion

    }
}
